package com.utils;

import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.car.common.model.Users;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.Db;

public class StringUtil {

	private static Connection  conn = null;
	
	public static Connection getConn() {
		if (conn == null) {
			try {
				Class.forName("com.mysql.jdbc.Driver");
				conn = DriverManager.getConnection(PropKit.get("jdbc.club.url"), PropKit.get("jdbc.club.username"),PropKit.get("jdbc.club.password"));
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}
		return conn;
	}
	
	/**
	 * 获取字符串中的数字
	 * @param str
	 * @return
	 */
	public static String getNumbers(String str){
		if (str != null) {
			String regEx="[^0-9]";   
			Pattern pattern = Pattern.compile(regEx);   
			Matcher matcher = pattern.matcher(str);   
			return matcher.replaceAll("").trim();
		}
		return "";
	}
	
	public static boolean empty(String str){
		return str == null || "null".equals(str) || str.trim().length() == 0;
	}
	
	public static String encode(String key) {
		try {
			return java.net.URLEncoder.encode(key , "utf-8");
		} catch (Exception e) {
			
		}   
		return null;
	}
	
	public static String decode(String key) {
		if (StringUtil.empty(key)) {
			return "";
		}
		try {
			return java.net.URLDecoder.decode(key , "utf-8");
		} catch (Exception e) {
			return "火星人";
		}   
	}
	
	private static boolean isNotEmojiCharacter(char codePoint)  { 
		return (codePoint == 0x0) || 
		(codePoint == 0x9) || 
		(codePoint == 0xA) || 
		(codePoint == 0xD) || 
		((codePoint >= 0x20) && (codePoint <= 0xD7FF)) || 
		((codePoint >= 0xE000) && (codePoint <= 0xFFFD)) || 
		((codePoint >= 0x10000) && (codePoint <= 0x10FFFF)); 
	}
	
	/** 
	* 过滤emoji 或者 其他非文字类型的字符 
	* @param source 
	* @return 
	*/ 
	public static String filterEmoji(String source) { 
		int len = source.length(); 
		StringBuilder buf = new StringBuilder(len); 
		for (int i = 0; i < len; i++) { 
			char codePoint = source.charAt(i); 
			if (isNotEmojiCharacter(codePoint))  { 
				buf.append(codePoint); 
			} else {
				buf.append("*");
			}
		} 
		return buf.toString(); 
	}
	
	/**
	 * 
	 * <option value="1">袁杰</option><option value="2">李扬</option><option value="3">张炜</option><option value="4">常少彪</option>
	 * @param person
	 * @return
	 */
	public static String getSQLForPerson(String person) {
		StringBuffer result = new StringBuffer(" and ( ");
		if ("1".equalsIgnoreCase(person)) {
			result.append(" area_id = 350000 or ");
			result.append(" area_id = 440000 or ");
			result.append(" area_id = 450000 or ");
			result.append(" area_id = 460000 or ");
			result.append(" area_id = 500000 or ");
			result.append(" area_id = 510000 or ");
			result.append(" area_id = 520000 or ");
			result.append(" area_id = 530000 or ");
			result.append(" area_id = 810000 or ");
			result.append(" area_id = 710000 or ");
			result.append(" area_id = 430000 or ");
			result.append(" area_id = 820000  ");
		} else if ("2".equalsIgnoreCase(person)) {
			result.append(" area_id = 110000 or ");
			result.append(" area_id = 120000 or ");
			result.append(" area_id = 150000 or ");
			result.append(" area_id = 210000 or ");
			result.append(" area_id = 220000 or ");
			result.append(" area_id = 230000 or ");
			result.append(" area_id = 370000 or ");
			result.append(" area_id = 360000 ");
		} else if ("3".equalsIgnoreCase(person)) {
			result.append(" area_id = 140000 or ");
			result.append(" area_id = 130000 or ");
			result.append(" area_id = 540000 or ");
			result.append(" area_id = 610000 or ");
			result.append(" area_id = 620000 or ");
			result.append(" area_id = 630000 or ");
			result.append(" area_id = 640000 or ");
			result.append(" area_id = 650000 or ");
			result.append(" area_id = 120001 or ");
			result.append(" area_id = 9934863 or ");
			result.append(" area_id = 123879 or ");
			result.append(" area_id = 7654321 or ");
			result.append(" area_id = 362718 or ");
			result.append(" area_id = 2718593 or ");
			result.append(" area_id = 190311 or ");
			result.append(" area_id = 410000 or ");
			result.append(" area_id = 281973233 ");
		} else if ("4".equalsIgnoreCase(person)) {
			result.append(" area_id = 310000 or ");
			result.append(" area_id = 320000 or ");
			result.append(" area_id = 330000 or ");
			result.append(" area_id = 340000 or ");
			result.append(" area_id = 420000 ");
			
		}
		result .append(" )");
		return result.toString();
	}
	
	/**
	 * 
	 * <option value="1">袁杰</option><option value="2">李扬</option><option value="3">张炜</option><option value="4">常少彪</option>
	 * @param person
	 * @return
	 */
	public static String getSQLForPerson2(String person) {
		StringBuffer result = new StringBuffer(" and ( ");
		if ("1".equalsIgnoreCase(person)) {
			result.append(" ta.area_id = 350000 or ");
			result.append(" ta.area_id = 440000 or ");
			result.append(" ta.area_id = 450000 or ");
			result.append(" ta.area_id = 460000 or ");
			result.append(" ta.area_id = 500000 or ");
			result.append(" ta.area_id = 510000 or ");
			result.append(" ta.area_id = 520000 or ");
			result.append(" ta.area_id = 530000 or ");
			result.append(" ta.area_id = 810000 or ");
			result.append(" ta.area_id = 710000 or ");
			result.append(" ta.area_id = 430000 or ");
			result.append(" ta.area_id = 820000  ");
		} else if ("2".equalsIgnoreCase(person)) {
			result.append(" ta.area_id = 110000 or ");
			result.append(" ta.area_id = 120000 or ");
			result.append(" ta.area_id = 150000 or ");
			result.append(" ta.area_id = 210000 or ");
			result.append(" ta.area_id = 220000 or ");
			result.append(" ta.area_id = 230000 or ");
			result.append(" ta.area_id = 370000 or ");
			result.append(" ta.area_id = 360000 ");
		} else if ("3".equalsIgnoreCase(person)) {
			result.append(" ta.area_id = 140000 or ");
			result.append(" ta.area_id = 130000 or ");
			result.append(" ta.area_id = 540000 or ");
			result.append(" ta.area_id = 610000 or ");
			result.append(" ta.area_id = 620000 or ");
			result.append(" ta.area_id = 630000 or ");
			result.append(" ta.area_id = 640000 or ");
			result.append(" ta.area_id = 650000 or ");
			result.append(" ta.area_id = 120001 or ");
			result.append(" ta.area_id = 9934863 or ");
			result.append(" ta.area_id = 123879 or ");
			result.append(" ta.area_id = 7654321 or ");
			result.append(" ta.area_id = 362718 or ");
			result.append(" ta.area_id = 2718593 or ");
			result.append(" ta.area_id = 190311 or ");
			result.append(" ta.area_id = 410000 or ");
			result.append(" ta.area_id = 281973233 ");
		} else if ("4".equalsIgnoreCase(person)) {
			result.append(" ta.area_id = 310000 or ");
			result.append(" ta.area_id = 320000 or ");
			result.append(" ta.area_id = 330000 or ");
			result.append(" ta.area_id = 340000 or ");
			result.append(" ta.area_id = 420000 ");
		}
		result .append(" )");
		return result.toString();
	}
	
	public static String getSQLForPerson3(String person) {
		StringBuffer result = new StringBuffer(" and ( ");
		if ("1".equalsIgnoreCase(person)) {
			result.append(" card_users.area_id = 350000 or ");
			result.append(" card_users.area_id = 440000 or ");
			result.append(" card_users.area_id = 450000 or ");
			result.append(" card_users.area_id = 460000 or ");
			result.append(" card_users.area_id = 500000 or ");
			result.append(" card_users.area_id = 510000 or ");
			result.append(" card_users.area_id = 520000 or ");
			result.append(" card_users.area_id = 530000 or ");
			result.append(" card_users.area_id = 810000 or ");
			result.append(" card_users.area_id = 710000 or ");
			result.append(" card_users.area_id = 430000 or ");
			result.append(" card_users.area_id = 820000  ");
		} else if ("2".equalsIgnoreCase(person)) {
			result.append(" card_users.area_id = 110000 or ");
			result.append(" card_users.area_id = 120000 or ");
			result.append(" card_users.area_id = 150000 or ");
			result.append(" card_users.area_id = 210000 or ");
			result.append(" card_users.area_id = 220000 or ");
			result.append(" card_users.area_id = 230000 or ");
			result.append(" card_users.area_id = 370000 or ");
			result.append(" card_users.area_id = 360000 ");
		} else if ("3".equalsIgnoreCase(person)) {
			result.append(" card_users.area_id = 140000 or ");
			result.append(" card_users.area_id = 130000 or ");
			result.append(" card_users.area_id = 540000 or ");
			result.append(" card_users.area_id = 610000 or ");
			result.append(" card_users.area_id = 620000 or ");
			result.append(" card_users.area_id = 630000 or ");
			result.append(" card_users.area_id = 640000 or ");
			result.append(" card_users.area_id = 650000 or ");
			result.append(" card_users.area_id = 120001 or ");
			result.append(" card_users.area_id = 9934863 or ");
			result.append(" card_users.area_id = 123879 or ");
			result.append(" card_users.area_id = 7654321 or ");
			result.append(" card_users.area_id = 362718 or ");
			result.append(" card_users.area_id = 2718593 or ");
			result.append(" card_users.area_id = 190311 or ");
			result.append(" card_users.area_id = 410000 or ");
			result.append(" card_users.area_id = 281973233 ");
		} else if ("4".equalsIgnoreCase(person)) {
			result.append(" card_users.area_id = 310000 or ");
			result.append(" card_users.area_id = 320000 or ");
			result.append(" card_users.area_id = 330000 or ");
			result.append(" card_users.area_id = 340000 or ");
			result.append(" card_users.area_id = 420000 ");
		}
		result .append(" )");
		return result.toString();
	}
	
	

	/**
	 * 
	 * <option value="1">袁杰</option><option value="2">李扬</option><option value="3">张炜</option><option value="4">常少彪</option>
	 * @param person
	 * @return
	 */
	public static String getSQLForPerson4(String person) {
		StringBuffer result = new StringBuffer(" and ( ");
		if ("1".equalsIgnoreCase(person)) {
			result.append(" me.area_id = 350000 or ");
			result.append(" me.area_id = 440000 or ");
			result.append(" me.area_id = 450000 or ");
			result.append(" me.area_id = 460000 or ");
			result.append(" me.area_id = 500000 or ");
			result.append(" me.area_id = 510000 or ");
			result.append(" me.area_id = 520000 or ");
			result.append(" me.area_id = 530000 or ");
			result.append(" me.area_id = 810000 or ");
			result.append(" me.area_id = 710000 or ");
			result.append(" me.area_id = 430000 or ");
			result.append(" me.area_id = 820000  ");
		} else if ("2".equalsIgnoreCase(person)) {
			result.append(" me.area_id = 110000 or ");
			result.append(" me.area_id = 120000 or ");
			result.append(" me.area_id = 150000 or ");
			result.append(" me.area_id = 210000 or ");
			result.append(" me.area_id = 220000 or ");
			result.append(" me.area_id = 230000 or ");
			result.append(" me.area_id = 370000 or ");
			result.append(" me.area_id = 360000 ");
			
		} else if ("3".equalsIgnoreCase(person)) {
			result.append(" me.area_id = 140000 or ");
			result.append(" me.area_id = 130000 or ");
			result.append(" me.area_id = 540000 or ");
			result.append(" me.area_id = 610000 or ");
			result.append(" me.area_id = 620000 or ");
			result.append(" me.area_id = 630000 or ");
			result.append(" me.area_id = 640000 or ");
			result.append(" me.area_id = 650000 or ");
			result.append(" me.area_id = 120001 or ");
			result.append(" me.area_id = 9934863 or ");
			result.append(" me.area_id = 123879 or ");
			result.append(" me.area_id = 7654321 or ");
			result.append(" me.area_id = 362718 or ");
			result.append(" me.area_id = 2718593 or ");
			result.append(" me.area_id = 190311 or ");
			result.append(" me.area_id = 410000 or ");
			result.append(" me.area_id = 281973233 ");
		} else if ("4".equalsIgnoreCase(person)) {
			result.append(" me.area_id = 310000 or ");
			result.append(" me.area_id = 320000 or ");
			result.append(" me.area_id = 330000 or ");
			result.append(" me.area_id = 340000 or ");
			result.append(" me.area_id = 420000 ");
		}
		result .append(" )");
		return result.toString();
	}
	public static void main(String[] args) {
		
	}
}
